package com.cj.bk_app.ui.profile

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.cj.bk_app.R
import com.cj.bk_app.model.bean.User
import com.cj.bk_app.view.compose.Line
import com.google.gson.Gson
import com.cj.bk_app.util.Preference
import com.cj.bk_app.util.toast


@Composable
fun ProfilePage() {
    val hasLogin by Preference(Preference.IS_LOGIN, false)
    val userJson by Preference(Preference.USER_GSON, "")
    var user: User? = null

    val isLogin by remember {
        mutableStateOf(hasLogin)
    }

    if (isLogin) {
        user = Gson().fromJson(userJson, User::class.java)
    }
    val current = LocalContext.current
    ProfileScreen(
        isLogin,
        user,
        onLogin = {
            current.toast(current.getString( R.string.developing))
        })
}

@Composable
fun ProfileScreen(
    isLogin: Boolean,
    user: User?,
    onLogin: () -> Unit,
) {
    Column {
        LoginMenu(isLogin = isLogin, user, clickLogin = onLogin)
        Line()
        Menu(stringResource(R.string.settings)) {}
        Line()
        Menu(stringResource(R.string.update_version)) {}
        Line()
        Menu(stringResource(R.string.about)) {}
        Line()
        VersionTv()
    }
}

@Composable
fun Menu(text: String, onClick: () -> Unit) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .height(50.dp)
            .clickable { onClick() }, verticalArrangement = Arrangement.Center
    ) {
        Text(
            text = AnnotatedString(text),
            fontSize = 16.sp,
            color = Color.Black,
            modifier = Modifier.padding(start = 10.dp)
        )
    }
}

@Composable
fun VersionTv() {

    val context = LocalContext.current
    Box(contentAlignment = Alignment.Center, modifier = Modifier
        .fillMaxWidth()
        .clickable {
        }) {
        Text(
            text = "Book V 1.0.0",
            fontSize = 14.sp,
            color = Color.Black,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.padding(top = 20.dp)
        )
    }
}

@Composable
fun LoginMenu(isLogin: Boolean, user: User? = null, clickLogin: () -> Unit) {
    Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier
        .padding(10.dp)
        .fillMaxWidth()
        .height(80.dp)
        .clickable {
            clickLogin()
        }) {

        if (isLogin) {
            AsyncImage(
                model = ImageRequest.Builder(LocalContext.current)
                    .data(user?.icon)
                    .crossfade(true)
                    .build(),
                placeholder = painterResource(R.drawable.ic_launcher),
                error = painterResource(R.drawable.ic_launcher),
                contentDescription = stringResource(R.string.login),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .clip(CircleShape)
                    .width(48.dp)
                    .height(48.dp)
            )
        } else {
            Image(
                painter = painterResource(R.drawable.ic_launcher),
                contentDescription = "",
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .clip(CircleShape)
                    .width(48.dp)
                    .height(48.dp)
            )
        }

        Text(
            text = if (isLogin) user?.username ?: "" else stringResource(id = R.string.loginOrRegister), color = Color.Black,
            fontSize = 18.sp,
            modifier = Modifier.padding(start = 24.dp)
        )


    }
}

@Preview
@Composable
fun ProfileScreenPreview() {
    ProfileScreen(
        false,
        null,
        onLogin = {})
}

private fun showFeedBackMenu(context: Context) {

}

private fun showMe(context: Context) {

}

fun Context.openBrowser(url: String) {
    Intent(Intent.ACTION_VIEW, Uri.parse(url)).run { startActivity(this) }
}